<?php
class page{

        /**
         * 当前页数
         *
         * @private integer
         */
            private $curpage;

        /**
         * 总页数
         *
         * @private integer
         */
            private $totalpage;

        /**
         * 查询的起始数
         *
         * @private integer
         */
            private $firstcount;

        /**
         * 显示每页条数
         *
         * @private integer
         */
            private $displaypage;

        /**
         * 起始页码数
         *
         * @private integer
         */
            private $startpage;

        /**
         * 结束页码数
         *
         * @private integer
         */
            private $endpage;

        /**
         * 信息总条数
         *
         * @private integer
         */
            private $total;

        /**
         * 页码数
         *
         * @private integer
         */
            private $pagelen;
            /*
             * $urlnow当前页面url
             */
            private $urlnow;

        /**
         * 构造函数
         */
        //total：信息总条数
        //displaypage：每页显示信息的条数
        //pagelen：需要显示的页码数
        public function  __construct($total, $urlnow,$displaypage = '3', $pagelen = '8'){
            $this->total = $total;
            $this->displaypage = $displaypage;
            $this->pagelen = $pagelen;
            $this->urlnow=$urlnow;
            $this->set();
            $this->output();
        }

        /**
         * 设置,计算分页
         */
        public function set(){

            //取得当前页数的值
            if($_GET['page']){
                $this->curpage=$_GET['page'];
            }else{
                //默认为首页
                $this->curpage=1;
            }

            //计算出起始查询数
            //分页的公式是:（当前页数 - 1 ）* 每页条数 , 每页条数
            $this->firstcount = ($this->curpage-1)*$this->displaypage;

            //计算出总页数
            if($this->total%$this->displaypage == 0){
                $this->totalpage = $this->total/$this->displaypage;
            }else{
                $this->totalpage = ceil($this->total/$this->displaypage);
            }

            //计算页码个数,页码总是等于奇数
            $this->pagelen = $this->pagelen%2?$this->pagelen:$this->pagelen+1;

            //计算页码偏移数
            $pageoffset = ($this->pagelen-1)/2;

            //分页数大于页码个数时可以偏移
            if($this->totalpage>$this->pagelen){
                //如果当前页小于等于偏移数
                if($this->curpage<=$pageoffset){
                    //起始页码数
                    $this->startpage = 1;
                    //结束页码数
                    $this->endpage = $this->pagelen;
                }else{
                    //如果当前页码右偏移超出最大分页数
                    //也就是当前页+偏移数>总页数
                    if($this->curpage+$pageoffset>$this->totalpage){
                        $this->startpage = $this->totalpage-$this->pagelen+1;
                        $this->endpage = $this->totalpage;
                    }else{
                        //左右偏移都存在时的计算
                        $this->startpage = $this->curpage - $pageoffset;
                        $this->endpage = $this->curpage + $pageoffset;
                    }
                }
            }
            //总页数小于页码数
            else{
                $this->startpage = 1;
                $this->endpage = $this->totalpage;
            }

            //用于select...limit...查询
            //为使函数外部可以访问这里的“$displaypg和firstcount”，将它们设为全局变量。注意一个变量重新定义为全局变量后，原值被覆盖，所以这里给它重新赋值。
            $GLOBALS["firstcount"]=$this->firstcount;
            $GLOBALS["displaypage"]=$this->displaypage;

        }

        /**
         * 分页结果输出
         */
        public function output(){

            //储存页码导航
            $pagenav = '';

            //打印总页数
            $pagenav .= "共<b>{$this->totalpage}</b>页";
            //打印总条数
            $pagenav .= "<b>{$this->total}</b>条 ";

            //首页
            if($this->totalpage>1){
                $pagenav .= "<a href=".$this->urlnow."&page=" . (1) . ">首页</a>";
            }

            //上一页
            if($this->curpage>1){
                $pagenav .= " <a href=".$this->urlnow."&page=" . ($this->curpage-1) . ">上一页</a> ";
            }

            //循环出页码
            if($this->totalpage>1){
                for($i=$this->startpage;$i<=$this->endpage;$i++){
                    $pagenav .= " " . " <a href=".$this->urlnow."&page=" . $i . ">{$i}</a>";
                 }
            }

            //下一页
            if($this->curpage<$this->totalpage){
                $pagenav .= "<a href=".$this->urlnow."&page=" . ($this->curpage+1) . ">下一页</a>";
            }

            //末页
            if($this->totalpage>1){
                $pagenav .= " <a href=".$this->urlnow."&page=" . $this->totalpage . ">末页</a> ";
            }

            /**
             * 输出页码导航
             *
             * @global string
             */
            $GLOBALS['pagenav'] = $pagenav;
        }

    }
?>